    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.dao.impl;

import cc.altius.dao.AmbulanceDao;
import cc.altius.domain.mapper.AmbulanceRowMapper;
import cc.altius.exception.CouldNotSaveToDataBaseException;
import cc.altius.model.Ambulance;
import cc.altius.model.AmbulanceSer;
import java.util.List;
import javax.sql.DataSource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author sagar
 */
@Repository("AmbulanceDao")
public class AmbulanceDaoImpl implements AmbulanceDao {

    
    @Autowired
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;
    private SessionFactory sessionFactory;
    private HibernateTemplate hibernateTemplate;
    
    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Override
    @Transactional
    public int addAmbulance(Ambulance ambulance) {
        return (Integer) this.hibernateTemplate.save(ambulance);
    }

    @Override
    @Transactional
    public Ambulance getAmbulanceById(int ambulanceId) {
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Ambulance where ambulanceId=" + ambulanceId);
        return (Ambulance) query.uniqueResult();
    }

    @Override
    @Transactional
    public int updateAmbulance(Ambulance ambulance, int ambulanceId) {
        Query query;
        Session session = this.sessionFactory.getCurrentSession();
       //Date date = DateUtils.getCurrentDateObject(DateUtils.IST);
        try {
            String hql = "Update "
                    + " Ambulance as amb"
                    + " set "
                    + " amb.companyName=:companyName,"
                    + " amb.notes=:notes,"
                    + " amb.contact1=:contact1,"
                    + " amb.contact2=:contact2,"
                    + " amb.contact3=:contact3"
                    + " WHERE amb.ambulanceId=:ambulanceId";
            query = session.createQuery(hql);
            query.setParameter("companyName", ambulance.getCompanyName());
            query.setParameter("contact1", ambulance.getContact1());
            query.setParameter("contact2", ambulance.getContact2());
            query.setParameter("contact3", ambulance.getContact3());
            query.setParameter("notes", ambulance.getNotes());
            query.setParameter("ambulanceId", ambulance.getAmbulanceId());
            int rowCount = query.executeUpdate();
            return rowCount;
        } catch (Exception e) {
            throw new CouldNotSaveToDataBaseException("Failed to add Ambulance because" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public List<Ambulance> getAmbulanceList() {
        List<Ambulance> ambulance = this.hibernateTemplate.find("from Ambulance");
        return ambulance;
    }

    @Override
    public Ambulance getAmbulanceByCompanyName(String companyName) {
       String sqlString = "SELECT ambulance.* FROM ambulance WHERE ambulance.COMPANY_NAME=?";
        Object params[] =  new Object[]{companyName};
        try {
           // LogUtils.systemLogger.info(LogUtils.buildStringForSystemLog(sqlString, params));
            return this.jdbcTemplate.queryForObject(sqlString, params, new AmbulanceRowMapper());
        } catch (EmptyResultDataAccessException erda) {
            //LogUtils.systemLogger.info(LogUtils.buildStringForSystemLog("No User found with username"));
            return null;
        }
    }

   
}
